package com.xiyou.weblog.web.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyou.weblog.common.domain.dos.ArticleDO;
import com.xiyou.weblog.common.domain.dos.ArticleTagRelDO;
import com.xiyou.weblog.common.domain.dos.TagDo;
import com.xiyou.weblog.common.domain.mapper.ArticleMapper;
import com.xiyou.weblog.common.domain.mapper.ArticleTagRelMapper;
import com.xiyou.weblog.common.domain.mapper.TagMapper;
import com.xiyou.weblog.common.utils.PageResponse;
import com.xiyou.weblog.common.utils.Response;
import com.xiyou.weblog.web.model.vo.tag.FindTagArticlePageListReqVO;
import com.xiyou.weblog.web.model.vo.tag.FindTagArticlePageListRspVO;
import com.xiyou.weblog.web.model.vo.tag.FindTagListRspVO;
import com.xiyou.weblog.web.service.TagService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xiyou
 * @description 首页标签模块
 */
@Service
@Slf4j
public class TagServiceImpl implements TagService {

    @Resource
    private TagMapper tagMapper;
    @Resource
    private ArticleTagRelMapper articleTagRelMapper;
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 获取标签列表
     */
    @Override
    public Response findTagList() {
        // 查询所有标签
        List<TagDo> tagDOS = tagMapper.selectList(Wrappers.emptyWrapper());

        // DO 转 VO
        List<FindTagListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(tagDOS)) {
            vos = tagDOS.stream()
                    .map(tagDO -> FindTagListRspVO.builder()
                            .id(tagDO.getId())
                            .name(tagDO.getName())
                            .build())
                    .toList();
        }

        return Response.success(vos);
    }


    /**
     * 获取标签下的所有文章
     */
    @Override
    public Response findTagPageList(FindTagArticlePageListReqVO findTagArticlePageListReqVO) {
        Long current = findTagArticlePageListReqVO.getCurrent();
        Long size = findTagArticlePageListReqVO.getSize();
        //标签ID
        Long tagId = findTagArticlePageListReqVO.getId();

        //判断是否存在
        TagDo tagDo = tagMapper.selectById(tagId);
        if (Objects.isNull(tagDo)) {
            log.warn("==> 该标签不存在，tag：{}", tagId);
            return PageResponse.fail();
        }

        // 查询所有包含该标签的文章ID
        List<ArticleTagRelDO> articleTagRelDOS = articleTagRelMapper.selectByTagId(tagId);

        // 是否存在文章
        if (CollectionUtils.isEmpty(articleTagRelDOS)) {
            log.warn("==> 该标签未发任何文章，tagId：{}",tagId);
            return PageResponse.fail();
        }

        //提取所有文章ID
        List<Long> articleIds = articleTagRelDOS.stream().map(ArticleTagRelDO::getArticleId).toList();

        // 根据文章 ID 获取查询文章分页信息
        Page<ArticleDO> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDO> articleDOS = page.getRecords();


        // DO 转 VO
        List<FindTagArticlePageListRspVO> vos = null;
        if (CollectionUtils.isNotEmpty(articleDOS)) {
            vos = articleDOS.stream().map(articleDO -> FindTagArticlePageListRspVO.builder()
                    .title(articleDO.getTitle())
                    .createDate(articleDO.getCreateTime().toLocalDate())
                    .cover(articleDO.getCover())
                    .id(articleDO.getId())
                    .build())
                    .toList();
        }

        return PageResponse.success(page, vos);
    }
}
